import {
  deleteFault,
  detailFault,
  disableFault,
  faultsMobileDisable,
  faultsUpdateReportAnalysisStatus,
  faultsUpdateVisibleType,
  gteFaultList,
} from '@/api/knowledge'
import { ref } from 'vue'

export const useGetFaults = () => {
  // 获取故障列表加载
  const getFaultsLoading = ref(false)

  // 获取故障列表请求参数
  const getFaultsParam = ref<KnownAPIRequest.KnownFaultList>({
    faultReason: '',
    limit: 50,
    name: '',
    oneFaultTypeId: '',
    page: 0,
    solution: '',
    threeFaultTypeId: '',
    twoFaultTypeId: '',
  })

  // 故障列表总条数
  const total = ref(0)

  // 故障列表
  const faults = ref<Array<KnowFaults>>([])

  /**
   * 获取故障列表
   */
  const getFaults = async () => {
    getFaultsLoading.value = true
    const res = await gteFaultList(getFaultsParam.value)
    getFaultsLoading.value = false
    total.value = Number(res.data.data.total)
    faults.value = res.data.data.rows
  }

  return {
    total,
    faults,
    getFaultsParam,
    getFaultsLoading,
    getFaults,
  }
}

export const useFaultsDel = () => {
  const deleteLoading = ref(false)

  const deleteFaults = async (ids: Array<string>) => {
    deleteLoading.value = true
    const res = await deleteFault(ids)
    deleteLoading.value = false
    return res.data
  }

  return {
    deleteLoading,
    deleteFaults,
  }
}

export const useGetFaultDetail = () => {
  const getFaultDetailLoading = ref(false)
  const faultDetail = ref<KnowFaultDetail>({
    code: '',
    delFaultReasonIds: [],
    delSolutionIds: [],
    faultImages: [],
    faultReasons: [],
    faultReasonCode: '',
    goodsTypeVos: [],
    id: '',
    mobileShow: true,
    name: '',
    oneFaultTypeId: '',
    reportAnalysis: true,
    solutions: [],
    solutionCode: '',
    solutionImages: [],
    status: false,
    threeFaultTypeId: '',
    twoFaultTypeId: '',
    visibilityType: 1,
  })

  const getFaultDetail = async (id: string) => {
    getFaultDetailLoading.value = true
    const res = await detailFault(id)
    faultDetail.value = res.data.data.row
    if (!faultDetail.value.delFaultReasonIds) {
      faultDetail.value.delFaultReasonIds = []
    }
    if (!faultDetail.value.delSolutionIds) {
      faultDetail.value.delSolutionIds = []
    }
    if (faultDetail.value.faultReasons.length === 0) {
      faultDetail.value.faultReasons = [
        {
          id: '',
          content: '',
        },
      ]
    }
    if (faultDetail.value.solutions.length === 0) {
      faultDetail.value.solutions = [
        {
          id: '',
          content: '',
        },
      ]
    }
    getFaultDetailLoading.value = false
  }

  return {
    faultDetail,
    getFaultDetailLoading,
    getFaultDetail,
  }
}

export const useFaultsDisable = () => {
  const disableLoading = ref(false)

  const faultsDisable = async (ids: Array<string>) => {
    disableLoading.value = true
    const res = await disableFault(ids, true)
    disableLoading.value = false
    return res.data
  }

  return {
    disableLoading,
    faultsDisable,
  }
}

export const useFaultsEnable = () => {
  const enableLoading = ref(false)

  const faultsEnable = async (ids: Array<string>) => {
    enableLoading.value = true
    const res = await disableFault(ids, false)
    enableLoading.value = false
    return res.data
  }

  return {
    enableLoading,
    faultsEnable,
  }
}

export const useFaultsMobileShow = () => {
  const mobileShowLoading = ref(false)

  const faultMobileShow = async (ids: Array<string>) => {
    mobileShowLoading.value = true
    const res = await faultsMobileDisable(ids, true)
    mobileShowLoading.value = false
    return res.data
  }

  return {
    faultMobileShow,
    mobileShowLoading,
  }
}

export const useFaultsMobileHide = () => {
  const mobileHideLoading = ref(false)

  const faultMobileHide = async (ids: Array<string>) => {
    mobileHideLoading.value = true
    const res = await faultsMobileDisable(ids, false)
    mobileHideLoading.value = false
    return res.data
  }

  return {
    faultMobileHide,
    mobileHideLoading,
  }
}

export const useFaultsReportAnalysisEnable = () => {
  const enableReportAnalysisLoading = ref(false)

  const enableReportAnalysis = async (ids: Array<string>) => {
    enableReportAnalysisLoading.value = true
    const res = await faultsUpdateReportAnalysisStatus(ids, true)
    enableReportAnalysisLoading.value = false
    return res.data
  }

  return {
    enableReportAnalysisLoading,
    enableReportAnalysis,
  }
}

export const useFaultsReportAnalysisDisable = () => {
  const disableReportAnalysisLoading = ref(false)

  const disableReportAnalysis = async (ids: Array<string>) => {
    disableReportAnalysisLoading.value = true
    const res = await faultsUpdateReportAnalysisStatus(ids, false)
    disableReportAnalysisLoading.value = false
    return res.data
  }

  return {
    disableReportAnalysisLoading,
    disableReportAnalysis,
  }
}

export const useFaultsUpdateVisibleType = () => {
  const updateViisbleTypeLoading = ref(false)

  const updateVisible = async (ids: Array<string>, visibleType: 1 | 2 | 3) => {
    updateViisbleTypeLoading.value = true
    const res = await faultsUpdateVisibleType(ids, visibleType)
    updateViisbleTypeLoading.value = false
    return res.data
  }

  return {
    updateViisbleTypeLoading,
    updateVisible,
  }
}
